﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Data;
using Saturn.AppSQLite;
using System.Data.SQLite;

namespace Saturn.App.FormOper
{
    class FrmLoginOper
    {
        //*****************以下为操作xml代码逻辑

        public string file = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Data.xml";

        /// <summary>
        /// 检查xml配置文件是不是存在
        /// </summary>
        /// <returns></returns>
        public List<string> CheckXml()
        {
            //不存在就创建
            if (!File.Exists(file))
            {
                XDocument xmlFile = new XDocument();
                XElement xel = new XElement("root");
                xmlFile.Add(xel);
                xmlFile.Save(file);

                return null;
            }
            else //存在就读取出来
            {
                XDocument xmlFile = XDocument.Load(file);
                var select = from c in xmlFile.Elements("root").Elements("userName") select c.Attribute("name");
                if (select.Count() > 0)
                {
                    List<string> lists = new List<string>();
                    foreach (var item in select)
                    {
                        lists.Add(item.Value);
                    }
                    return lists;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 写入xml一个新的Name
        /// </summary>
        /// <param name="userName"></param>
        public void InsertXml(string userName)
        {
            //先检查下是不是已经存在了
            if (CheckUser(userName))//存在就直接操作啦
            {
                DeleXml(userName);
                //return;
            }

            XDocument xmlFile = XDocument.Load(file);
            XElement root = xmlFile.Root;
            XElement name = new XElement("userName");
            XAttribute xat = new XAttribute("name", userName);
            name.Add(xat);
            root.Add(name);
            root.Save(file);
        }

        /// <summary>
        /// 检查是不是存在用户名
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool CheckUser(string userName)
        {
            XDocument xmlFile = XDocument.Load(file);
            var select = from c in xmlFile.Elements("root").Elements("userName")
                         where c.Attribute("name").Value == userName
                         select c.Attribute("name");
            return select.Count() > 0;
        }


        /// <summary>
        /// 删除没有勾选的用户名
        /// </summary>
        /// <param name="p"></param>
        internal void DeleXml(string userName)
        {
            XDocument xmlFile = XDocument.Load(file);
            var select = from c in xmlFile.Elements("root").Elements("userName")
                         where c.Attribute("name").Value == userName
                         select c.Attribute("name");
            if (select.Count() > 0)
            {
                XElement root = XDocument.Load(file).Root;
                root.Elements("userName").Where(c => c.Attribute("name").Value == userName).Remove();
                root.Save(file);
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool UserLogin(string userName, string userPwd)//, out string OrgCatID, out string OrgID, out string userID, out string realName, out string MainOrgID)
        {
            bool aa = false;
            if (userName=="1"&userPwd=="1")
            {
                aa = true;
            }
            return aa;
            //string strSql = string.Format("select * from SysUser where UserName='{0}' and Password='{1}'", userName, Common.MD5Encrypt.GetMD5Str(userPwd).ToUpper());
            //SQLiteCommand cmd = new SQLiteCommand();
            //cmd.CommandText = strSql;
            //cmd.CommandType = CommandType.Text;
            //DataTable dt = SqliteHelper.ExecuteDataSet(SqliteHelper.SqlConnection, cmd).Tables[0];
            //// 用户组织机构的类别:用来判断是生产线使用还是仓库使用
            //OrgCatID = dt.Rows.Count == 1 ? dt.Rows[0]["OrgCatID"].ToString() : "";
            //OrgID = dt.Rows.Count == 1 ? dt.Rows[0]["OrgID"].ToString() : "";
            //userID = dt.Rows.Count == 1 ? dt.Rows[0]["ID"].ToString() : "";
            //realName = dt.Rows.Count == 1 ? dt.Rows[0]["RealName"].ToString() : "";
            //MainOrgID = dt.Rows.Count == 1 ? dt.Rows[0]["MainOrgID"].ToString() : "";

            //return dt.Rows.Count == 1;
        }

        public void ttt(DataTable dtuser)
        {
            for (int i = 0; i < dtuser.Rows.Count; i++)
            {
                string strSql = "insert int SysUser (RecNo,ID,UserName,Password,RealName,Gender,OrgCode,OrgID,OrgName,OrgCatID,OrgCatName,MainOrgID) valus("+(i+1)+",'" + dtuser.Rows[i]["ID"] + "','" + dtuser.Rows[i]["UserName"] + "','" + dtuser.Rows[i]["Password"] + "','" + dtuser.Rows[i]["RealName"] + "','" + dtuser.Rows[i]["Sex"] + "','" + dtuser.Rows[i]["OrgCode"] + "','" + dtuser.Rows[i]["OrgID"] + "','" + dtuser.Rows[i]["OrgName"] + "','" + dtuser.Rows[i]["OrgCatID"] + "','" + dtuser.Rows[i]["OrgCatName"] + "','" + dtuser.Rows[i]["MainOrgID"] + "')";
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.CommandText = strSql;
                cmd.CommandType = CommandType.Text;
                try
                {
                    SqliteHelper.ExecuteNonQuery(SqliteHelper.SqlConnection, cmd);
                }
                catch (Exception ex)
                {

                    throw;
                }
            }
        }

        /// <summary>
        /// 用户同步
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool UserDownLoad(string userName, string userPwd, out string OrgCatID, out string OrgID, out string userID, out string realName, out string MainOrgID)
        {
            string strSql = string.Format("select * from SysUser where UserName='{0}' and Password='{1}'", userName, Common.MD5Encrypt.GetMD5Str(userPwd).ToUpper());
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.CommandText = strSql;
            cmd.CommandType = CommandType.Text;
            DataTable dt = SqliteHelper.ExecuteDataSet(SqliteHelper.SqlConnection, cmd).Tables[0];
            // 用户组织机构的类别:用来判断是生产线使用还是仓库使用
            OrgCatID = dt.Rows.Count == 1 ? dt.Rows[0]["OrgCatID"].ToString() : "";
            OrgID = dt.Rows.Count == 1 ? dt.Rows[0]["OrgID"].ToString() : "";
            userID = dt.Rows.Count == 1 ? dt.Rows[0]["ID"].ToString() : "";
            realName = dt.Rows.Count == 1 ? dt.Rows[0]["RealName"].ToString() : "";
            MainOrgID = dt.Rows.Count == 1 ? dt.Rows[0]["MainOrgID"].ToString() : "";

            return dt.Rows.Count == 1;
        }
    }
}
